<template>
  <basic-container>
    <el-alert
    title="统计时间：可查询最近三个月的信息"
    type="warning"
    :closable="false" style="margin-bottom:10px;">
  </el-alert>
    <avue-crud :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
     >
      <template slot="menuRight">
        <el-button type="primary"
          size="small"
          @click="setTime('今天')"
          >今天
        </el-button>
        <el-button type="primary"
          size="small"
          @click="setTime('昨天')"
          >昨天
        </el-button>
        <el-button type="primary"
          size="small"
           @click="setTime('近七天')"
          >近七天
        </el-button>
        <el-button type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permission.platform_profit_export"
          @click="handleExport">导出
        </el-button>
      </template>
    </avue-crud>
    <div class="curd-foter">
      <div class="redFontBox">交易数量：<span class="redFont">{{exhibition(countData.tradeCount) || 0}}</span> 笔</div>
      <div class="redFontBox">总交易金额：<span class="redFont">{{exhibition(countData.totalTradAmount) || 0}}</span> 元</div>
      <div class="redFontBox">总手续费：<span class="redFont">{{exhibition(countData.totalRFee) || 0}}</span> 元</div>
      <div class="redFontBox">总D0手续费：<span class="redFont">{{exhibition(countData.totalRFeeD0) || 0}}</span> 元</div>
      <div class="redFontBox">总分润金额：<span class="redFont">{{exhibition(countData.totalProfitAmount) || 0}}</span> 元</div>
      <div class="redFontBox">总Do分润金额：<span class="redFont">{{exhibition(countData.totalProfitAmountD0) || 0}}</span> 元</div>
    </div>
  </basic-container>
</template>

<script>
import { getList, getListCount, getExport } from "@/api/rb-core/platformprofit/platform_profit";
import {mapGetters} from "vuex";
// import { getToken } from '@/util/auth';
import {dateFormat} from '@/util/date'
import { downloadFileExcel } from "@/util/util";

export default {
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        // height:'calc(50vh - 50px)',
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        editBtn:false,
        addBtn:false,
        delBtn:false,
        selection: true,
        searchMenuSpan: 24,
        columnBtn: false,
        labelWidth:160,        //弹出表单的标题的统一宽度
        searchLabelWidth: 160, //搜索框标题宽度
        menuWidth:100,         //操作栏宽度
        column: [
          {
            label: "订单号",
            prop: "rptOrderCode",
            type: "input",
            sortable: true,
      			search: true,
            searchOrder:7,
            width: 250,
            overHidden: true
  				},
          {
            label: "交易类型",
            prop: "rptTradeType",
            type: "select",
            sortable: true,
            search: true,
            searchOrder: 2,
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            dicUrl: "/api/blade-system/dict/dictionary?code=rpt_trade_type",
            width: 100,
            overHidden: true
          },
          {
            label: "支付方式",
            prop: "rptPayType",
            type: "select",
            sortable: true,
            search: true,
            searchOrder:2,
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            dataType: "number",
            dicUrl: "/api/blade-system/dict/dictionary?code=rpt_pay_type",
            width: 100,
            overHidden: true
  				},
          {
            label: "卡类型",
            prop: "cardType",
            type: "select",
            sortable: true,
            search: true,
            searchOrder: 7,
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "string",
            dicUrl: "/api/blade-system/dict/dictionary?code=card_type",
            width: 100,
            overHidden: true
          },
          {
            label: "分润订单类型",
            prop: "profitOrderType",
            type: "select",
            search: true,
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "string",
            dicUrl: "/api/blade-system/dict/dictionary?code=profit_order_type",
            width: 100,
            hide: true,
            display: false,
          },
          {
            label: "终端号",
            prop: "rtNo",
            type: "input",
            sortable: true,
      			search: true,
            searchOrder:9,
            width: 100,
            overHidden: true
  				},
          {
            label: "终端流水号",
            prop: "rptRtNo",
            type: "input",
            sortable: true,
            search: true,
            searchOrder:10,
            width: 200,
            overHidden: true
  				},
          {
            label: "商户名",
            prop: "rpiName",
            type: "input",
            sortable: true,
            search: true,
            searchOrder:4,
            width: 100,
            overHidden: true
  				},
          {
            label: "商户号",
            prop: "rpiNo",
            type: "input",
            sortable: true,
            search: true,
            searchOrder:5,
            width: 150,
            overHidden: true
  				},
          {
            label: "交易门店",
            prop: "rpsName",
            type: "input",
            sortable: true,
            search: true,
            searchOrder:6,
            width: 150,
            overHidden: true
  				},
          {
            label: "交易金额(元)",
            prop: "rptAmount",
            type: "input",
            sortable: true,
            search: true,
            searchOrder:11,
            width: 150,
            overHidden: true
  				},
          {
            label: "退款金额(元)",
            prop: "rptRefundAmount",
            type: "input",
            sortable: true,
            searchOrder: 11,
            width: 150,
            overHidden: true
          },
          {
            label: "总手续费(元)",
            prop: "rptFee",
            type: "input",
            sortable: true,
            width: 150,
            overHidden: true
          },
          {
            label: "总D0手续费(元)",
            prop: "rptFeeD0",
            type: "input",
            sortable: true,
            width: 150,
            overHidden: true
          },
          {
            label: "分润金额(元)",
            prop: "profitAmount",
            type: "input",
            sortable: true,
            width: 150,
            overHidden: true
          },
          {
            label: "Do分润(元)",
            prop: "profitAmountD0",
            type: "input",
            sortable: true,
            width: 150,
            overHidden: true
          },
          {
            label: "交易时间",
            prop: "rptPayTime",
            type: "select",
            sortable: true,
            width: 150,
            overHidden: true
  				},
          
          // {
          //   label: "支付类型",
          //   prop: "rptPayModel",
          //   type: "select",
          //   sortable: true,
          //   search: true,
          //   searchOrder:3,
          //   props: {
          //     label: "dictValue",
          //     value: "dictKey"
          //   },
          //   // hide:true,
          //   display:false,
          //   dataType: "number",
          //   dicUrl: "/api/blade-system/dict/dictionary?code=rpt_pay_model",
  				// },
          // {
          //   label: "交易状态",
          //   prop: "rptPayStateName",
          //   type: "input",
          //   sortable: true,
  				// },          
          {
            label: "统计时间段",
            prop: "timeData",
            type: "datetimerange",
            searchRange: true,
            defaultTime:['00:00:00', '23:59:59'],
            format:'yyyy-MM-dd HH:mm:ss',
            valueFormat:'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: "统计时间开始时间",
            endPlaceholder: "统计时间结束时间",
            search: true,
            rules: [
              {
                required: true,
                message: "请选择统计时间段",
                trigger: "blur",
              },
            ],
            hide: true,
            searchSpan: 12,
            display: false,
          },

        ]
      },
    data: [],
    countData:{}
  };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.platform_profit_add, false),
        viewBtn: this.vaildData(this.permission.platform_profit_view, false),
        delBtn: this.vaildData(this.permission.platform_profit_delete, false),
        editBtn: this.vaildData(this.permission.platform_profit_edit, false)
    };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
    exhibition() {
      return (val) => {
        if (val == "" || val == "-1") {
          return (val = 0);
        } else {
          return val;
        }
      };
    },
  },
   watch: {
    "search.timeData"(val){
      if(val){
        this.search.beginTime = val[0];
        this.search.endTime = val[1];
      }else{
        this.search.beginTime = "";
        this.search.endTime = "";
      }
    }
  },
  methods: {
    /**
     * @func 设置时间
     * @desc 点击获取点击天数时间
     * @param {Object} type - 今天，昨天，近七天
     * */
    setTime(type) {
      let date = new Date();
      this.query = {};
      if (type == "今天") {
        this.search.beginTime = dateFormat(date, "yyyy-MM-dd") +' 00:00:00';
        this.search.endTime = dateFormat(date, "yyyy-MM-dd") +' 23:59:59';
        this.search.timeData = [this.search.beginTime, this.search.endTime];
        this.page.currentPage = 1;
        this.forInSearch();
        this.refreshChange();
      } else if (type == "昨天") {
        this.search.beginTime = dateFormat(new Date(date.getTime() - 3600 * 1000 * 24 * 1),"yyyy-MM-dd")+' 00:00:00';
        this.search.endTime = dateFormat(new Date(date.getTime() - 3600 * 1000 * 24 * 1),"yyyy-MM-dd")+' 23:59:59';
        this.search.timeData = [this.search.beginTime, this.search.endTime];
        this.page.currentPage = 1;
        this.forInSearch();
        this.refreshChange();
      } else {
        // "本月"
        this.search.beginTime = dateFormat(date,"yyyy-MM")+'-01 00:00:00';
        this.search.endTime = dateFormat(date,"yyyy-MM-dd")+' 23:59:59';
        this.search.timeData = [this.search.beginTime, this.search.endTime];
        this.page.currentPage = 1;
        this.forInSearch();
        this.refreshChange();
      }
    },
    forInSearch(){
      for(let key in this.search){
        //console.log(key)
        if(this.search[key]){
          if(key.indexOf("$") == -1 ){
            this.query[key] = this.search[key]
          }
        }
      }
    },
    beforeOpen(done, type) {
      // if (["edit", "view"].includes(type)) {
      //   getDetail(this.form.id).then(res => {
      //     this.form = res.data.data;
      //   });
      // }
      done();
    },
    searchReset() {
      this.query = {};
      this.search = {};
      this.$set(this.search, 'timeData', ["",""]);
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage){
      this.page.currentPage = currentPage;
      // this.refreshChange()
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
      // this.refreshChange()
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });

      // 获取统计
      getListCount(Object.assign(params, this.query)).then(res => {
        this.countData = res.data.data;
      });

    },
    handleExport() {
      if (this.search.timeData[0]) {
        this.search.beginTime = this.search.timeData[0];
      }
      if (this.search.timeData[1]) {
        this.search.endTime = this.search.timeData[1];
      }
      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        getExport(this.search).then((res) => {
            downloadFileExcel(res);
          },
          (error) => {
            window.console.log(error);
          }
        );
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop
        this.query.descs = ""
      } else {
        //倒序
        this.query.descs = val.prop
        this.query.ascs = ""
      }
      this.refreshChange();
    }
  }
};
</script>

<style lang="scss" scoped>
.curd-foter{
  padding-top: 10px;
  display: flex;
  flex-wrap: wrap;
}

.redFontBox{
  margin-right: 40px;
  .redFont{
    color:red;
  }
}
</style>
